package com.gradecak.alfresco.mvc.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.alfresco.service.namespace.QName;

import com.gradecak.alfresco.mvc.action.Action;


public class CMObject {

  private final Map<QName, Object> propertiesMap = new HashMap<QName, Object>();
  private final List<QName> aspects = new LinkedList<QName>();
  private final List<Action> actionList = new ArrayList<Action>();
  private final List<QName> readOnlyProperties = new ArrayList<QName>();

  private final Boolean acceptNullProperties;

  public CMObject() {
    this.acceptNullProperties = Boolean.FALSE;
  }

  public CMObject(Boolean acceptNull) {
    this.acceptNullProperties = Boolean.TRUE;
  }


  public Map<QName, Object> getPropertiesMap() {
    return new HashMap<QName, Object>(propertiesMap);
  }

  public void setProperties(Map<QName, Object> propertiesMap) {
    if (propertiesMap != null) {
      for (QName property : propertiesMap.keySet()) {
        this.propertiesMap.put(property, propertiesMap.get(property));
      }
    }
  }

  public void setProperty(QName qname, Object value) {
    if (this.acceptNullProperties == Boolean.FALSE) {
      if (qname != null && value != null) {
        this.propertiesMap.put(qname, value);
      }
    }
    else {
      if (qname != null) {
        this.propertiesMap.put(qname, value);
      }
    }
  }

  public Object getProperty(QName qname) {
    return qname != null ? this.propertiesMap.get(qname) : null;
  }

  public List<QName> getAspects() {
    return aspects;
  }

  public void setAspects(List<QName> aspects) {
    if (aspects != null) {
      this.aspects.addAll(aspects);
    }
  }

  public List<Action> getActions() {
    return actionList;
  }

  public void addAction(Action action) {
    if (action != null) {
      this.actionList.add(action);
    }
  }

  public List<QName> getReadOnlyPropertiess() {
    return readOnlyProperties;
  }

  public boolean isReadOnlyProperty(QName property) {
    return property != null ? readOnlyProperties.contains(property) : false;
  }

  public void addReadOnlyProperty(QName property) {
    if (property != null) {
      readOnlyProperties.add(property);
    }
  }
}
